<?php


namespace App\Http\Controllers\Forwarder;


use App\Http\Controllers\Controller;
use App\Models\Account;
use App\Models\AccountFund;
use App\Models\Coupon;
use App\Models\CouponUser;
use Illuminate\Http\Request;

class CouponController extends Controller
{
    public function index(Request $request)
    {
        $query = Coupon::where('forwarder_id', $request->user()->forwarder_id);
        $list = $query->get();
        return $this->response($list);
    }

    public function couponUser(Request $request)
    {
        $user_id = $request->user()->id;
        $query = Coupon::with(['coupon_user' => function ($q) use ($user_id) {
            $q->where('user_id', $user_id);
        }])->where('forwarder_id', $request->user()->forwarder_id);
        $list = $query->get();
        return $this->response($list);
    }

    public function award($id, Request $request)
    {
        $user_id = $request->user()->id;
        $obj = CouponUser::where('id', $id)->where('user_id', $user_id)->where('status', 1)->first();
        if (!is_object($obj)) {
            abort(5091);
        }
        $coupon = Coupon::where('id', $obj->coupon_id)->first();
        if ($obj->award_type === 0) {
            $account = Account::where('user_id', $user_id)->first();
            $accountFund = new AccountFund();
            $usable_amount = $coupon->pea + $account->usable_amount;
            $amount = $coupon->pea + $account->amount;
            $accountFund->fill([
                'user_id' => $account->user_id,
                'account_id' => $account->id,
                'title' => "活动送智慧",
                'rid' => $obj->id,
                'type' => 1,
                'amount' => $coupon->pea,
                'reckon' => 1,
                'before_amount' => $account->amount,
                'later_amount' => $amount,
            ]);
            $accountFund->save();
            $account->usable_amount = $usable_amount;
            $account->amount = $amount;
            $account->save();
        }
        $obj->status = 2;
        $obj->save();
        return $this->response();
    }

    public function view($id, Request $request)
    {
        $data = Coupon::where('id', $id)->first();
        return $this->response($data);
    }

    public function store(Request $request, Coupon $obj)
    {
        $all = $request->all();
        $all['end_time'] = $all['end_time'] . ' 23:59:59';
        $obj->fill($all);
        $obj->title = Coupon::getTitle($obj);
        $obj->user_id = $request->user()->id;
        $obj->forwarder_id = $request->user()->forwarder_id;
        $obj->save();
        return $this->response($obj->id);
    }

    public function update($id, Request $request)
    {
        $obj = Coupon::where('forwarder_id', $request->user()->forwarder_id)->where('id', $id)->first();
        $obj->update($request->all());
        $obj->title = Coupon::getTitle($obj);
        $obj->save();
        return $this->response();
    }

    public function destroy($id)
    {
        $obj = Coupon::find($id);
        $obj->delete();
        return $this->response();
    }
}
